package main

import (
	"cmp"
	"fmt"
	"io"
	"strings"
)

func main() {
	//负权环_v0.png
	f := NewForwardStart("A", "B", "C", "D", "E", "F", "G", "H")
	f.AddEdge("A", "B", 1, true)
	f.AddEdge("A", "C", 4, true)
	f.AddEdge("A", "D", 7, true)
	f.AddEdge("B", "H", 1, true)
	f.AddEdge("H", "G", 2, true)
	f.AddEdge("G", "E", 1, true)
	f.AddEdge("E", "D", 1, true)
	f.AddEdge("D", "F", 3, true)
	f.AddEdge("F", "B", 9, true)
	f.AddEdge("D", "C", 3)
	f.AddEdge("C", "D", -3)
	f.AddEdge("B", "E", -4)
	f.AddEdge("E", "B", 5)

	var start, end string
	for {
		fmt.Print("Enter start,end node name:")
		_, err := fmt.Scanln(&start, &end)
		if err == io.EOF {
			break
		}
		if err != nil {
			fmt.Println(err)
			continue
		}
		start = strings.ToUpper(start)
		end = strings.ToUpper(end)

		if !f.existNode(start) || !f.existNode(end) {
			fmt.Println("不存在节点！")
			continue
		}

		outs := f.FindOutEdges(start, func(a, b *Edge) int {
			return -cmp.Compare(a.w, b.w)
		})
		fmt.Println(start, "出边：", outs)
		outs = f.FindOutEdges(end, func(a, b *Edge) int {
			return -cmp.Compare(a.w, b.w)
		})
		fmt.Println(end, "出边：", outs)

		h, p, d := f.ShortestHeight(start)
		fmt.Printf("%s--->%s step:%d, walk:%s, dist:%.0f\n", start, end, h[f.GetNodeNumber(end)], f.Restore(start, end, p), d[f.GetNodeNumber(end)])
	}

}
